package com.xique.client.service.biz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xique.client.api.IClientPassApi;
import com.xique.client.bean.domain.ParkDeviceNew;
import com.xique.client.bean.vo.WxAppParkStatisticVO;
import com.xique.client.service.biz.service.IWxAppHomeBizService;
import com.xique.client.service.service.IParkDeviceNewService;
import com.xique.client.service.service.IPayOrderService;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.sys.api.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信首页接口service
 */
@Service
@Slf4j
public class WxAppBizHomeServiceImpl implements IWxAppHomeBizService {

    @Autowired
    private IPayOrderService payOrderService;

    @Autowired
    private IClientPassApi clientPassApi;

    @Autowired
    private IParkDeviceNewService parkDeviceNewService;

    /**
     * 查询该用户具有权限的所有车场
     * 备注：如果是车场管理员就只有绑定车场可看；如果是服务商账号，则可以看名下所有车场；
     * @param userId
     * @return
     */
    @Override
    public AjaxResult getParkList(Long userId) {
        SysUser sysUser = new SysUser(userId);
        return null;
    }

    @Override
    public AjaxResult getParkStatistic() {

        Long parkId = SecurityUtils.getParkId();

        WxAppParkStatisticVO vo = new WxAppParkStatisticVO();

        // 电子支付统计
        Double electronic = payOrderService.selectElectronicStatistic(parkId);
        vo.setElectronic(electronic);

        // 现金收费统计
        Double cash = payOrderService.selectCashStatistic(parkId);
        vo.setCash(cash);

        // 未收金额统计
        Double wait = payOrderService.selectWaitStatistic(parkId);
        vo.setWait(wait);

        // 免费金额统计
        Double free = payOrderService.selectFreeStatistic(parkId);
        vo.setFree(free);

        // 剩余车位统计
        Integer remain = 0;
        JSONObject object = clientPassApi.space(SecurityUtils.getParkId());
        if (0 == Integer.parseInt(object.get("code").toString())) {
            Map<String, Object> map = (HashMap) object.get("data");
            remain = Integer.parseInt(map.get("remainSpaces").toString());
        }
        vo.setRemain(remain);

        // 离线设备统计
        Integer count = 0;
        ParkDeviceNew search = new ParkDeviceNew();
        search.setParkId(parkId);
        List<ParkDeviceNew> list = parkDeviceNewService.selectParkDeviceNewList(search);
        for (ParkDeviceNew item : list) {
            //将在线状态为空的设备也认定为离线。
            if (item.getOnlineStatus() == null){
                count++;
                continue;
            }
            Map<String, Object> map = JSONObject.parseObject(item.getOnlineStatus(), Map.class);
            if (null != map && "offline".equals(map.get("state"))) {
                count++;
            }
        }
        vo.setOffLine(count);

        return AjaxResult.success(vo);
    }
}
